<?php
/**
 *  SMW_properties
 *
 *  This extension is intended to help the semantic media wiki editor
 *      to define some properties as mandatory, and later remind that 
 *      requirement when editing or creating a new page of that category
 *
 *  The spected format for properties declaration in category pages:
 *       {{#property:NameOfProperty|type|[required|optional]}}
 *
 *
 * @author Jorge Condomi
 */

/**
 * Protect against register_globals vulnerabilities.
 * This line must be present before any global variable is referenced.
 */
if( !defined( 'MEDIAWIKI' ) ) {
	echo( "This is an extension to the MediaWiki package and cannot be run standalone.\n" );
	die( -1 );
}

// Extension credits that will show up on Special:Version
$wgExtensionCredits['other'][] = array(
	'name' => 'SMW_Properties',
	'version' => '1.0',
	'author' => 'Jorge Condomi',
	'url' => 'http://www.mediawiki.org/wiki/Extension:SMW_Properties',
	'description' => 'Extension to allow a better control of the categories properties.',
);


//Main directory of this extension
$SMW_P_dir = dirname(__FILE__)."/";

$wgAutoloadClasses['SMW_PropertiesParser'] = $SMW_P_dir."includes/SMW_PropertiesParser.hooks.php";
$wgHooks['LanguageGetMagic'][] = 'SMW_PropertiesParser::RegisterHook';

$wgHooks['ParserFirstCallInit'][] = 'SMW_properties_setup';

//$wgHooks['EditPage::attemptSave'][] = 'fnMyHook';
$wgHooks['EditFilter'][] = 'SWM_propertiesDispatch';


function SMW_properties_setup( &$parser){
    global $wgOut;
    $parser->setFunctionHook( 'property', 'SMW_PropertiesParser::Parse' );
    $wgOut->addStyle( "../extensions/SMW_Properties/css/SMW_properties.common.css", 'screen');
    return true;
}


    
function SWM_propertiesDispatch(  $editor, $text, $section, &$error, $summary){
    global $wgOut, $wgParser;
     
    $popts = new ParserOptions();
    $popts->setTidy(true);
    $popts->enableLimitReport();
    $output = $wgParser->parse($text,Title::newFromText('DummieTextPrueba'), $popts);
    
    $prop_temp = SMWPropertyValue::makeProperty( '_INST' );
    $categories = $output->mSMWData->getPropertyValues($prop_temp);
    $SMWdata =  $output->mSMWData;
    $SMWP_parser = new SMW_PropertiesParser();
    $SMWP_error = array();
    
    foreach($categories as $cat => $data){
        $dbKeys = $data->getDBkeys();
        $t = Title::newFromDBKey($cat);
        $a = new Article($t);
        $a->loadContent();
        $content = $a->getContent();
        $properties = $SMWP_parser->extractProperties($content);
        foreach($SMWdata->getProperties() as $key => $prop){
            $key_normalized = strtolower($key);
            if(isset($properties[$key_normalized])){
                unset($properties[$key_normalized]);
            }
        }
        
        // No property missed
        if(!count($properties)) continue;
        foreach($properties as $key => $prop){
            if(!isset($SMWP_error[$cat])) $SMWP_error[$cat] = array();
            if($prop[2] != "optional" ) $SMWP_error[$cat][] = $prop;
        }
    }
    
    if(count($SMWP_error) > 0){
       // Dealing with the missing properties here
       foreach($SMWP_error as $key => $props){
           $t_c = Title::newFromText($key);
           $link_c = $t_c->getFullURL();
           $error .= '<div class="SMWP_properties_error_list"><span class="error_category">Properties for ['.$link_c.' '.$key.'] missed:</span><ul>';
           foreach($props as $prop){
               $name = $prop[0];
               $t = Title::newFromText("Property:".$name);
               $link = $t->getFullURL();
               $error .= '<li><span class="error_property">['.$link.' '.$name.'] ('.$prop[1].')</span></li>';
           }
           $error .= '</ul></div>';
        }
    }
    return true;
}


?>